package com.demo.java.rxjava;

import com.demo.java.utils.LogUtils;
import com.demo.java.utils.ThreadUtils;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;

import static com.demo.java.utils.LogUtils.logt;

public class RxioTest {

    public static void main(String[] args) {
        Observable<String> observable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<String> emitter) throws Exception {
                for (int i = 0; i < 10; i++) {
                    System.out.println(logt("produce: " + i));
                    emitter.onNext(String.valueOf(i));
                }
                emitter.onComplete();
            }
        });
        observable.subscribeOn(Schedulers.io())
                .observeOn(Schedulers.newThread())
                .subscribe(s -> {
                    System.out.println(logt("consumer: " + s));
                });

        ThreadUtils.sleep(10);
    }
}
